package com.zlzlib.libui.util;

import android.content.Context;
import android.media.AudioManager;

import androidx.annotation.IntDef;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * @Desc: 声音控制工具
 * @Copyright:
 * @DateTime: 2021/11/3 11:17
 * @Author zlz
 * @Version 1.0
 */
public class AudioManagerHelper {

    private final AudioManager audioManager;
    private int NOW_AUDIO_TYPE = AudioManager.STREAM_MUSIC;
    private int NOW_FLAG = FLAG_NOTHING;
    private int VOICE_STEP_100 = 2; //加减音量0-100的步进。

    /**
     * 封装：声音类型
     * 媒体，闹钟，铃声，通话，系统声,通知，无障碍
     */
    @IntDef({AudioManager.STREAM_MUSIC, AudioManager.STREAM_ALARM, AudioManager.STREAM_RING,
            AudioManager.STREAM_VOICE_CALL, AudioManager.STREAM_SYSTEM,
            AudioManager.STREAM_NOTIFICATION, AudioManager.STREAM_ACCESSIBILITY})
    @Retention(RetentionPolicy.SOURCE)
    public @interface TYPE {
    }

    /**
     * 封装：修改声音的标志
     * 修改时弹出提示，修改时播放声音
     */
    public final static int FLAG_NOTHING = 0;

    @IntDef({AudioManager.FLAG_SHOW_UI, AudioManager.FLAG_PLAY_SOUND, FLAG_NOTHING})
    @Retention(RetentionPolicy.SOURCE)
    public @interface FLAG {
    }

    @IntDef({AudioManager.MODE_NORMAL,
            AudioManager.MODE_RINGTONE,
            AudioManager.MODE_IN_CALL,
            AudioManager.MODE_IN_COMMUNICATION})
    @Retention(RetentionPolicy.SOURCE)
    public @interface MODE {
    }

    public AudioManagerHelper(Context context) {
        audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    }

    /**
     * @param mode 设置播放模式
     */
    public void setMode(@MODE int mode) {
        if (mode == audioManager.getMode()) {
            return;
        }
        audioManager.setMode(mode);
    }

    /**
     * @return 当前类型声音的最大音量
     */
    public int getSystemMaxVolume() {
        return audioManager.getStreamMaxVolume(NOW_AUDIO_TYPE);
    }

    /**
     * @return 当前类型声音的大小
     */
    public int getSystemCurrentVolume() {
        return audioManager.getStreamVolume(NOW_AUDIO_TYPE);
    }

    /**
     * 以0-100为范围，获取当前的音量值
     *
     * @return 获取当前的音量值
     */
    public int get100CurrentVolume() {
        return 100 * getSystemCurrentVolume() / getSystemMaxVolume();
    }

    /**
     * 修改步进值
     *
     * @param step 步进值
     * @return this
     */
    public AudioManagerHelper setVoiceStep100(int step) {
        VOICE_STEP_100 = step;
        return this;
    }

    /**
     * @param type 模式
     * @return 改变当前的模式，对全局API生效
     */
    public AudioManagerHelper setAudioType(@TYPE int type) {
        NOW_AUDIO_TYPE = type;
        return this;
    }

    /**
     * @param flag 当前FLAG
     * @return 改变当前FLAG，对全局API生效
     */
    public AudioManagerHelper setFlag(@FLAG int flag) {
        NOW_FLAG = flag;
        return this;
    }

    /**
     * @return 渐进式音量加
     */
    public AudioManagerHelper addVoiceSystem() {
        audioManager.adjustStreamVolume(NOW_AUDIO_TYPE, AudioManager.ADJUST_RAISE, NOW_FLAG);
        return this;
    }

    /**
     * @return 渐进式音量减
     */
    public AudioManagerHelper subVoiceSystem() {
        audioManager.adjustStreamVolume(NOW_AUDIO_TYPE, AudioManager.ADJUST_LOWER, NOW_FLAG);
        return this;
    }

    /**
     * 调整音量，自定义
     *
     * @param num 0-100
     * @return 改完后的音量值
     */
    public int setVoice100(int num) {
        int a = (int) Math.ceil((num) * getSystemMaxVolume() * 0.01);
        a = a <= 0 ? 0 : a;
        a = a >= 100 ? 100 : a;
        audioManager.setStreamVolume(NOW_AUDIO_TYPE, a, 0);
        return get100CurrentVolume();
    }

    /**
     * 步进加，步进值可修改
     * 0——100
     *
     * @return 改完后的音量值
     */
    public int addVoice100() {
        int a = (int) Math.ceil((VOICE_STEP_100 + get100CurrentVolume()) * getSystemMaxVolume() * 0.01);
        a = a <= 0 ? 0 : a;
        a = a >= 100 ? 100 : a;
        audioManager.setStreamVolume(NOW_AUDIO_TYPE, a, NOW_FLAG);
        return get100CurrentVolume();
    }

    /**
     * 步进减，步进值可修改
     * 0——100
     *
     * @return 改完后的音量值
     */
    public int subVoice100() {
        int a = (int) Math.floor((get100CurrentVolume() - VOICE_STEP_100) * getSystemMaxVolume() * 0.01);
        a = a <= 0 ? 0 : a;
        a = a >= 100 ? 100 : a;
        audioManager.setStreamVolume(NOW_AUDIO_TYPE, a, NOW_FLAG);
        return get100CurrentVolume();
    }
}
